package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.EquipmentStatus;
import com.ruoyi.system.domain.RepairManagement;
import com.ruoyi.system.mapper.EquipmentStatusMapper;
import com.ruoyi.system.mapper.RepairManagementMapper;
import com.ruoyi.system.service.IEquipmentStatusService;
import com.ruoyi.system.service.IRepairManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 故障报修管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class RepairManagementServiceImpl implements IRepairManagementService {
    @Autowired
    private RepairManagementMapper repairManagementMapper;
    @Autowired
    private IEquipmentStatusService iEquipmentStatusService;
    @Autowired
    private EquipmentStatusMapper equipmentStatusMapper;

    /**
     * 查询故障报修管理
     *
     * @param repairCode 故障报修管理主键
     * @return 故障报修管理
     */
    @Override
    public RepairManagement selectRepairManagementByRepairCode(String repairCode) {
        return repairManagementMapper.selectRepairManagementByRepairCode(repairCode);
    }

    /**
     * 查询故障报修管理列表
     *
     * @param repairManagement 故障报修管理
     * @return 故障报修管理
     */
    @Override
    public List<RepairManagement> selectRepairManagementList(RepairManagement repairManagement) {
        return repairManagementMapper.selectRepairManagementList(repairManagement);
    }

    /**
     * 新增故障报修管理
     *
     * @param repairManagement 故障报修管理
     * @return 结果
     */
    @Override
    public int insertRepairManagement(RepairManagement repairManagement) {
        repairManagement.setRepairCode(IdUtils.fastSimpleUUID());
        repairManagement.setReporterId(SecurityUtils.getUserId().toString());
        repairManagement.setReporterName(SecurityUtils.getLoginUser().getUser().getNickName());
        return repairManagementMapper.insertRepairManagement(repairManagement);
    }

    /**
     * 修改故障报修管理
     *
     * @param repairManagement 故障报修管理
     * @return 结果
     */
    @Override
    public int updateRepairManagement(RepairManagement repairManagement) {
        repairManagement.setUpdateTime(DateUtils.getNowDate());
        repairManagement.setStatus("已处理");
        repairManagementMapper.updateRepairManagement(repairManagement);
        EquipmentStatus equipmentStatus = new EquipmentStatus();
        equipmentStatus.setEquipmentId(repairManagement.getDeviceId());
        equipmentStatus.setStatus("正常");
        equipmentStatusMapper.updateEquipmentStatus(equipmentStatus);
        return 1;
    }

    /**
     * 批量删除故障报修管理
     *
     * @param repairCodes 需要删除的故障报修管理主键
     * @return 结果
     */
    @Override
    public int deleteRepairManagementByRepairCodes(String[] repairCodes) {
        return repairManagementMapper.deleteRepairManagementByRepairCodes(repairCodes);
    }

    /**
     * 删除故障报修管理信息
     *
     * @param repairCode 故障报修管理主键
     * @return 结果
     */
    @Override
    public int deleteRepairManagementByRepairCode(String repairCode) {
        return repairManagementMapper.deleteRepairManagementByRepairCode(repairCode);
    }

    @Override
    public List<RepairManagement> xialaxuan() {
        List<RepairManagement> xialaxuan = repairManagementMapper.xialaxuan();
        return xialaxuan;
    }
}
